package com.ruoyi.service;

import com.google.protobuf.InvalidProtocolBufferException;
import com.ruoyi.domain.alarm.AlarmBp;
import com.ruoyi.domain.alarm.AlarmHr;
import com.ruoyi.domain.alarm.AlarmSpo2;
import com.ruoyi.domain.alarm.DeviceAIarm;
import com.ruoyi.protobuf.AlarmInfo;
import com.ruoyi.protobuf.RealtimeData;
import com.ruoyi.utils.MyDateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Component

public class NewAlarmDataParser implements IDataParser {
    private static final Logger log = LoggerFactory.getLogger(NewAlarmDataParser.class);
    @Autowired
    private SaveInfoToRedisService saveInfoToRedisService;
    @Override
    public void DataParse(byte[] payload,String deviceId) {
        AlarmInfo.Alarm_infokConfirm alarm_infokConfirm = null;
        DeviceAIarm deviceAIarm = new DeviceAIarm();
        try {
            alarm_infokConfirm = AlarmInfo.Alarm_infokConfirm.parseFrom(payload);
        } catch(InvalidProtocolBufferException ex) {
            ex.printStackTrace();
            return;
        }
        if(alarm_infokConfirm.hasAlarm()){
            AlarmInfo.HealthAlarmV3 alarmV3 = alarm_infokConfirm.getAlarm();
            //hr warn
            List<AlarmInfo.HrAlarmV3> hrAlarmList = alarmV3.getAlarmHrList();
            if(hrAlarmList!=null && hrAlarmList.size()>0){
                List<AlarmHr> alarmHrs = new ArrayList<>();
                for(int i=0;i<hrAlarmList.size();i++){
                    AlarmInfo.HrAlarmV3 hrAlarm = hrAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(hrAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("hr warn time:%s, hr:%d",alarmTime,hrAlarm.getHr()));
                    AlarmHr alarmHr = new AlarmHr();
                    alarmHr.setTimeStamp(alarmTime);
                    alarmHr.setHr(hrAlarm.getHr());
                    alarmHrs.add(alarmHr);
                }
            }

            //spo2 warn

            List<AlarmInfo.Spo2AlarmV3> spo2AlarmList = alarmV3.getAlarmSpo2List();
            if(spo2AlarmList!=null && spo2AlarmList.size()>0){
                List<AlarmSpo2> spo2Alarms = new ArrayList<>();
                for(int i=0;i<spo2AlarmList.size();i++){
                    AlarmInfo.Spo2AlarmV3 spo2Alarm = spo2AlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(spo2Alarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("spo2 alarm time:%s, boxy:%d",alarmTime,spo2Alarm.getSpo2()));
                    AlarmSpo2 alarmSpo2 = new AlarmSpo2();
                    alarmSpo2.setTimeStamp(alarmTime);
                    alarmSpo2.setSpo2(spo2Alarm.getSpo2());

                    spo2Alarms.add(alarmSpo2);
                }
                deviceAIarm.setAlarmSpo2s(spo2Alarms);
            }

            //thrombus warn
            List<AlarmInfo.ThrombusAlarmV3> thrombusAlarmList = alarmV3.getAlarmThrombusList();
            if(thrombusAlarmList!=null && thrombusAlarmList.size()>0){
                for(int i=0;i<thrombusAlarmList.size();i++){
                   AlarmInfo.ThrombusAlarmV3 thrombusAlarmV3 = thrombusAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(thrombusAlarmV3.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("thrombus alarm time:%s",alarmTime));

            }

            //the guy fall down
            List<AlarmInfo.FallAlarmConfV3> fallAlarmList = alarmV3.getAlarmFallList();
            if(fallAlarmList!=null && fallAlarmList.size()>0){
                for(int i=0;i<fallAlarmList.size();i++){
                    AlarmInfo.FallAlarmConfV3 fallAlarm = fallAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(fallAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("fall down alarm time:%s",alarmTime));
                }
            }

            //temperature alarm
            List<AlarmInfo.TemperatureConf> tempAlarmList = alarmV3.getAlarmTemperatureList();
            if(tempAlarmList!=null && tempAlarmList.size()>0){
                for(int i=0;i<tempAlarmList.size();i++){
                    AlarmInfo.TemperatureConf tempAlarm = tempAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(tempAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("temperature alarm time:%s,temperature:%d",alarmTime,tempAlarm.getTemperature()));
                }
            }

            //bp alarm
            List<AlarmInfo.BpConf> bpAlarmList = alarmV3.getAlarmBpList();
            if(bpAlarmList!=null && bpAlarmList.size()>0){
                List<AlarmBp> alarmBps = new ArrayList<>();
                for(int i=0;i<bpAlarmList.size();i++){
                    AlarmInfo.BpConf bpAlarm = bpAlarmList.get(i);
                    String alarmTime = MyDateTimeUtils.convert2DateTimeString(bpAlarm.getTimeStamp().getDateTime().getSeconds());
                    log.info(String.format("bp alarm time:%s,sbp:%d,dbp:%d",alarmTime,bpAlarm.getSbp(),bpAlarm.getDbp()));
                    AlarmBp alarmBp = new AlarmBp();
                    alarmBp.setTimeStamp(alarmTime);
                    alarmBp.setSbp(bpAlarm.getSbp());
                    alarmBp.setDbp(bpAlarm.getDbp());
                    alarmBps.add(alarmBp);
                }
                deviceAIarm.setAlarmBps(alarmBps);
            }


            }

            //sos alarm, will send an alarm without location first, before do locate/call
            if(alarmV3.hasSOSNotificationTime()){
                RealtimeData.DateTime sosTime = alarmV3.getSOSNotificationTime();
                String alarmTime = MyDateTimeUtils.convert2DateTimeString(sosTime.getDateTime().getSeconds());
                log.info(String.format("sos alarm time:%s",alarmTime));
            }
        }
        if(alarm_infokConfirm.hasAlarminfo()){
            com.ruoyi.domain.alarm.AlarmInfo alarmInfo = new com.ruoyi.domain.alarm.AlarmInfo();
            AlarmInfo.AlarminfoV3 infoV3 = alarm_infokConfirm.getAlarminfo();
            String alarmTime = MyDateTimeUtils.convert2DateTimeString(infoV3.getTimeStamp().getDateTime().getSeconds());
            alarmInfo.setTimeStamp(alarmTime);
            //device in low power
            if(infoV3.hasLowpowerPercentage()){
                log.info(String.format("low power time:%s, battery:%d",alarmTime,infoV3.getLowpowerPercentage()));
                alarmInfo.setLowpowerPercentage(infoV3.getLowpowerPercentage());
            }
            //device is power off
            if(infoV3.hasPoweroffPercentage()){
                log.info(String.format("power off time:%s, battery:%d",alarmTime,infoV3.getPoweroffPercentage()));
                alarmInfo.setPoweroffPercentage(infoV3.getPoweroffPercentage());
            }
            //device not wear
            if(infoV3.hasWearstate()){
                log.info(String.format("not wear time:%s",alarmTime));
                alarmInfo.setWearstate(infoV3.getWearstate());
            }
            //intercept call
            if(infoV3.hasInterceptNumber()){
                log.info(String.format("intercept call time:%s, phone number:%s",alarmTime,infoV3.getInterceptNumber()));
                alarmInfo.setInterceptNumber(infoV3.getInterceptNumber());
            }
            deviceAIarm.setAlarmInfo(alarmInfo);
        }
        saveInfoToRedisService.saveAlarmToRedis(deviceId, deviceAIarm);
    }

}
